home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / FSEEK.C < prev    next >
C/C++ Source or Header  |  1992-03-02  |  2KB  |  86 lines

  1. /* This is file FSEEK.C */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. /* This file was modified 1991-10-20 by R. Davidson in order to correct
  8. ** for ascii text files.
  9. */
  10.  
  11. #if defined(LIBC_SCCS) && !defined(lint)
  12. static char sccsid[] = "@(#)fseek.c    5.3 (Berkeley) 3/9/86";
  13. #endif LIBC_SCCS and not lint
  14.  
  15. /*
  16.  * Seek for standard library.  Coordinates with buffering.
  17.  */
  18.  
  19. #include    <stdio.h>
  20.  
  21. long lseek();
  22.  
  23. fseek(iop, offset, ptrname)
  24.     register FILE *iop;
  25.     long offset;
  26. {
  27.     register resync, c;
  28.     long p = -1;            /* can't happen? */
  29.     long adjust = 0;
  30.     char *q, *qq;
  31.  
  32.     iop->_flag &= ~_IOEOF;
  33.     if (iop->_flag&_IOREAD) {
  34.         if (ptrname<2 && iop->_base &&
  35.             !(iop->_flag&_IONBF)) {
  36.             c = iop->_cnt;
  37.             p = offset;
  38.             if (ptrname==0) {
  39.                 long curpos = lseek(fileno(iop), 0L, 1);
  40.                 if (curpos == -1)
  41.                     return (-1);
  42.                 p += c - curpos;
  43.             } else
  44.                 offset -= c;
  45.             if(!(iop->_flag&_IORW) && c>0&&p<=c
  46.                 && p>=iop->_base-iop->_ptr){
  47.                 if (iop->_flag&_IOTEXT){
  48.                   q = &iop->_ptr[iop->_cnt];
  49.                   qq = iop->_ptr + (int)p;
  50.                   while (--q >= qq)
  51.                     if (*q == '\n'){
  52.                       adjust++; qq++;
  53.                     }
  54.                   p += adjust;
  55.                 }
  56.                 iop->_ptr += (int)p;
  57.                 iop->_cnt -= (int)p;
  58.                 return(0);
  59.             }
  60.             resync = offset&01;
  61.         } else
  62.             resync = 0;
  63.         if (iop->_flag & _IORW) {
  64.             iop->_ptr = iop->_base;
  65.             iop->_flag &= ~_IOREAD;
  66.             resync = 0;
  67.         }
  68.         p = lseek(fileno(iop), offset-resync, ptrname);
  69.         iop->_cnt = 0;
  70.         if (resync && p != -1)
  71.             if (getc(iop) == EOF)
  72.                 p = -1;
  73.     }
  74.     else if (iop->_flag & (_IOWRT|_IORW)) {
  75.         p = fflush(iop);
  76.         if (iop->_flag & _IORW) {
  77.             iop->_cnt = 0;
  78.             iop->_flag &= ~_IOWRT;
  79.             iop->_ptr = iop->_base;
  80.         }
  81.         return(lseek(fileno(iop), offset, ptrname) == -1 || p == EOF ?
  82.             -1 : 0);
  83.     }
  84.     return(p==-1?-1:0);
  85. }
  86.